package com.sinya.shoppingmall.fragment;

import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.TextView;

import com.sinya.shoppingmall.R;
import com.sinya.shoppingmall.implement.GetDataInterface;
import com.sinya.shoppingmall.ui.recomend.BuyNowListView;
import com.sinya.shoppingmall.widget.PagerSlidingTabStrip;

import java.util.ArrayList;
import java.util.List;

/**
 * 限时抢购
 *
 * @author KoizumiSinya
 * @date 2016/1/14.
 */
public class PanicBuyFragment extends BaseFragment implements GetDataInterface {

    private List<BuyNowListView> pViews;

    private TextView timeCounted, days, hour, second, minute;
    private PagerSlidingTabStrip tabStrip;
    private ViewPager viewPager;
    private ViewPageAdapter adapter;

    private int mDisplayDays;
    private int mDisplayHours;
    private int mDisplayMinutes;
    private int mDisplaySeconds;

    //tab内容是否显示单行
    Boolean isSingle = false;

    final Handler handler = new Handler() {          // handle
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:

                    if (viewPager.getCurrentItem() == 4) {
                        days.setVisibility(View.VISIBLE);
                        hour.setVisibility(View.VISIBLE);
                        second.setVisibility(View.VISIBLE);
                        minute.setVisibility(View.VISIBLE);

                        timeCounted.setText("距离本场结束:");
                        days.setText(String.valueOf(mDisplayDays) + "天 ");
                        hour.setText(String.valueOf(mDisplayHours) + ":");
                        second.setText(String.valueOf(mDisplayMinutes) + ":");
                        minute.setText(String.valueOf(mDisplaySeconds));
                    } else {
                        timeCounted.setText("本场抢购已结束了哦");
                        days.setVisibility(View.GONE);
                        hour.setVisibility(View.GONE);
                        second.setVisibility(View.GONE);
                        minute.setVisibility(View.GONE);
                    }
                    break;

                case 2:
                    days.setVisibility(View.GONE);
                    hour.setText(String.valueOf("00"));
                    second.setText(String.valueOf("00"));
                    minute.setText("00");
                    break;
            }

            super.handleMessage(msg);
        }
    };

    @Override
    protected View initView() {
        //TODO 必须继承的方法 用来返回布局文件
        return View.inflate(mContext, R.layout.fragment_buynow, null);
    }

    @Override
    protected void setView() {
        //TODO 必须继承的方法 用来设置初始化布局控件
        initViews();
        setData();
        countTime();
    }

    private void initViews() {
        tabStrip = (PagerSlidingTabStrip) rootView.findViewById(R.id.scroll_tab);
        tabStrip.setSlidingLose(mSlidingPaneLayout);
        //tabStrip.setOnTouchListener(touchListener);
        viewPager = (ViewPager) rootView.findViewById(R.id.view_group);
        viewPager.setOnTouchListener(touchListener);

        this.timeCounted = (TextView) rootView.findViewById(R.id.timeCounted);
        this.days = (TextView) rootView.findViewById(R.id.days);
        this.hour = (TextView) rootView.findViewById(R.id.hour);
        this.second = (TextView) rootView.findViewById(R.id.second);
        this.minute = (TextView) rootView.findViewById(R.id.minute);
    }

    @Override
    public void fristGetData() {
        //TODO 实现自GetDataInterface接口
//        VolleyRequest.Start(mContext, handler, 0, VolleyParams.limitbuy, "1", "10");
    }

    @Override
    public void updateData() {
        //TODO 实现自GetDataInterface接口
    }

// [-] Override Method

    private View.OnTouchListener touchListener = new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int act = event.getAction();
            if (act == MotionEvent.ACTION_DOWN) {
                // 处理侧滑冲突的问题
                if (mSlidingPaneLayout != null) {
                    mSlidingPaneLayout.requestDisallowInterceptTouchEvent(true);
                }

            }

            return false;
        }
    };

    public void setData() {
        //TODO 标题设置

        int u = 8;
        List<String> arrs = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            if (i == 4) {
                arrs.add(u++ + ":00\n抢购中");
            }
            arrs.add(u++ + ":00\n结束");
        }

        pViews = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            BuyNowListView view = new BuyNowListView(mContext, mSlidingPaneLayout);
            pViews.add(view);
        }
        adapter = new ViewPageAdapter(pViews, arrs);
        viewPager.setAdapter(adapter);
        tabStrip.setViewPager(viewPager);
        tabStrip.setTextSingle(isSingle);
        tabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //views.get(position).getData();
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    private void countTime() {

        Runnable runnable = new Runnable() {
            @Override
            public void run() {

                new CountDownTimer(864000000, 1000) {

                    @Override
                    public void onTick(long millisUntilFinished) {

                        mDisplayDays = (int) ((millisUntilFinished / 1000) / 86400);
                        mDisplayHours = (int) (((millisUntilFinished / 1000) - (mDisplayDays * 86400)) / 3600);
                        mDisplayMinutes = (int) (((millisUntilFinished / 1000) - ((mDisplayDays * 86400) + (mDisplayHours * 3600))) / 60);
                        mDisplaySeconds = (int) ((millisUntilFinished / 1000) % 60);

                        Animation an = new RotateAnimation(0.0f, 90.0f, 250f, 273f);
                        an.setFillAfter(true);

                        Message message = Message.obtain();
                        message.what = 1;
                        handler.sendMessage(message);

                    }

                    @Override
                    public void onFinish() {
                        //TODO: this is where you would launch a subsequent activity if you'd like.  I'swsm currently just setting the seconds to zero
                        Message message = Message.obtain();
                        message.what = 2;
                        handler.sendMessage(message);

                    }
                }.start();
            }
        };

        runnable.run();
    }

    // [+] Adapter
    class ViewPageAdapter extends PagerAdapter {

        private List<BuyNowListView> pViews;
        private List<String> titles;

        public ViewPageAdapter(List<BuyNowListView> pViews, List<String> titles) {
            this.pViews = pViews;
            this.titles = titles;
        }

        @Override
        public int getCount() {
            return pViews.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object obj) {
            return view == obj;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(pViews.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(pViews.get(position));
            return pViews.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }

        /*@Override
        public int getPageSelectedIconResId(int position) {
            return icons[position % icons.length];
        }

        @Override
        public int getTextSelectColor(int position) {
            return textSelectors[position % textSelectors.length];
        }*/
    }
// [-] Adapter
}
